package com.test.base.api;

import com.test.base.dao.NideshopCategoryDao;
import com.test.base.entity.NideshopCategory;
import com.test.base.service.CartService;
import com.test.base.utils.Assist;
import com.test.base.vo.api.CategoryVo;
import com.test.base.vo.common.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "api/catalog")
@Api(description = "获取分类栏目数据")
public class CatalogApi {
    @Autowired
    private NideshopCategoryDao nideshopCategoryDao;

    @ApiOperation(value = "获取分类栏目数据", notes = "")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "分类栏目ID", paramType = "query", required = true, dataType = "Long")
    })
    public ResultData index(Long id) {
        Assist ad = new Assist();
        ad.setRequires(ad.andEq("parent_id", 0));
        ad.setStartRow(0);
        ad.setRowSize(10);
        List<NideshopCategory> data = nideshopCategoryDao.selectNideshopCategory(ad);
        NideshopCategory currentCategory = null;
        if (null != id) {
            currentCategory = nideshopCategoryDao.selectNideshopCategoryById(id);
        }
        if(null == currentCategory){
            currentCategory = data.get(0);
        }
        if (null != currentCategory && currentCategory.getId() != null) {
            Assist subCategoryListAssist = new Assist();
            subCategoryListAssist.setRequires(Assist.andEq("parent_id", currentCategory.getId()));
            currentCategory.setSubCategoryList( nideshopCategoryDao.selectNideshopCategory(subCategoryListAssist));
        }
        CategoryVo categoryVo = new CategoryVo();
        categoryVo.setCategoryList(data);
        categoryVo.setCurrentCategory(currentCategory);
        return ResultData.success(categoryVo);
    }

    @ApiOperation(value = "获取分类栏目数据", notes = "")
    @RequestMapping(value = "/current", method = RequestMethod.GET)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "分类栏目ID", paramType = "query", required = true, dataType = "Long")
    })
    public ResultData current(Long id) {
        NideshopCategory currentCategory = null;
        if (null != id) {
            currentCategory = nideshopCategoryDao.selectNideshopCategoryById(id);
        }
        if (null != currentCategory && currentCategory.getId() != null) {
            Assist subCategoryListAssist = new Assist();
            subCategoryListAssist.setRequires(Assist.andEq("parent_id", currentCategory.getId()));
            currentCategory.setSubCategoryList( nideshopCategoryDao.selectNideshopCategory(subCategoryListAssist));
        }
        CategoryVo categoryVo = new CategoryVo();
        categoryVo.setCurrentCategory(currentCategory);
        return ResultData.success(categoryVo);
    }

}
